function trimAllElements(formobj) {
	var count = formobj.elements.length;
	for (var i = 0; i < count; i++) {
		var obj = formobj.elements[i];
		if (obj.type!="file"&&(obj.tagName == "INPUT" || obj.tagName == "TEXTAREA")) {
			var str = trim(obj.value);
			if (str != "" && str.length > 0) {
				obj.value = str.replace(/(^[\s　]*)|([\s　]*$)/g, "");
			} else {
				obj.value = str.replace(/(^[\s　]*)|([\s　]*$)/g, "");
			}
			//alert(obj.value);
		}
	}
}

// Removes leading whitespaces
function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim(value) {
	var str = LTrim(RTrim(value));
	if (str != "" && str.length > 0) {
		str = str.replace(/(^[\s　]*)|([\s　]*$)/g, "");
	}
	return str;
}

//add by alex.su@2008-5-6 for limiting the maxlength of textarea
function isMaxLen(o){
     var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";
     if(o.getAttribute && o.value.length>nMaxLen){
         o.value=o.value.substring(0,nMaxLen)
     }
}

function isElementChecked(elmName){
	var element1 = document.getElementsByName(elmName);
	if (element1 == null || element1 == "") {
		return false;
	}
	for (var i = 0; i < element1.length; i++) {
		if (element1[i].checked) {
			return true;
		}
	}
		return false;
}

function HtmlDecode(text) {
	return text.replace(/&amp;/g, "&").replace(/&quot;/g, "\"").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
}
function HtmlEncode(text) {
	return text.replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

/**
设置一组radio中值为value的radio为选中状态
**/
setRadioChecked = function(opts,value)
{
	for(var loop =0;loop<opts.length;loop++)
	{
		
		var opt = opts[loop];
		opt.selected = false;
		if( opt.value == value )
		{
			opt.selected = true;
		}
	}
}

/**
 * 1方法是针对select
 * radio 是checked
 */
setRadioChecked2 = function(radios,value){
	var flag = false;
	for(var loop =0;loop<radios.length;loop++)
	{
		var radio = radios[loop];
		radio.checked = false;
		if( radio.value == value )
		{
			flag = true;
			radio.checked = true;
		}
	}
	//if(!flag){
		//radios[0].checked=true;
	//}
}

setRadioChecked1 = function(opts,value)
{
	for(var loop =0;loop<opts.length;loop++)
	{
		var opt = opts[loop];
		opt.checked = false;
		if(value)
		{
			opt.checked = true;
		}
	}
}




function setCheckboxSameV(th,form,id){
     var list=eval("document."+form+"."+id);
	 if(list == null){
	 	return;
	 }
     if(list.type=="checkbox"){
     	if(th){
              list.checked=true;
            }else{
              list.checked=false;
          }

     }else{
 	   for(var i=0;i<list.length;i++){
           if(th){
              list[i].checked=true;
            }else{
              list[i].checked=false;
          }
       }
     }
}

function setSelect(selectId,valueArray,disabled){
	var selectH = document.getElementById(selectId);
	var len=selectH.options.length;
	for(var i=0; i<len; i++){
		var opt=selectH[i];
		if(isInArray(valueArray,opt.value)){
			selectH.selectedIndex=i;
			break;
		}
	}
	if(disabled&&disabled!=""){
		selectH.setAttribute("disabled",disabled);
	}else{
		selectH.removeAttribute("disabled");
	}
}

function setCheckbox(name,array,disabled){
	var elements = document.getElementsByName(name);
	for(var i=0; i<elements.length; i++){
		var ele = elements[i];
		if(isInArray(array,ele.value)){
			ele.setAttribute("checked","checked");
		}else{
			ele.setAttribute("checked","");
			ele.removeAttribute("checked");
		}
		if(disabled&&disabled!=""){
			ele.setAttribute("disabled",disabled);
		}else{
			ele.removeAttribute("disabled");
		}
	}
}

function isInArray(array,value){
	if(array==null){return false;}
	for(var i = 0; i< array.length; i++){
		if(value==array[i]){return true;}
	}
	return false;
}


function doPrint(){
	doPrintByElement("printSpan");
}

function doPrintByElement(elementId){
	var body = $(elementId).innerHTML;
  	document.body.innerHTML = "<center>" + body + "</center>";
	window.print();
	window.location.href=window.location.href;
	//window.history.go(0);
}


/**
*处理input回车事件，如果btn有效，这回车事件转化为btn.click()事件
**/
function processEnterKey(event,btn){
	if(event.keyCode!=13)return true;
	if( btn === undefined || btn === null ){
		return false;
	}
	var obj = $(btn);
	if( obj != undefined && obj != null ){
		$(btn).click();
	}
	return false
}

function closeMainHandler(){
	var handler = $("Main_handleBar");
	if( handler != undefined && handler != null ){
		handler.style.display = "none";
	}
}

function compareDate(date1,date2){
        var EQUAL   = 0; 
        var LESS    = 1; 
        var GREATER = 2;
        
        if(date1 == null || date1.length == 0 || date2 == null || date2.length == 0)
        	return EQUAL;
        
        // date format must be "YYYY-MM-DD"
        var _date1 = new Array();
        var _date2 = new Array();

        _date1 = date1.substr(0,10).split("-");
        _date2 = date2.substr(0,10).split("-");

        var d1 = new Date(_date1[0],_date1[1]-1,_date1[2],"00","00");
        var d2 = new Date(_date2[0],_date2[1]-1,_date2[2],"00","00");
        if (d1.getTime()<d2.getTime()) {
          return LESS;
        }else if(d1.getTime()> d2.getTime()){
          return GREATER;
        }else if(d1.getTime()== d2.getTime()){
          return EQUAL;
        }else{
        	return LESS;
        }
}
